package com.ssd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Strings;
import com.ssd.common.VMSystem;
import com.ssd.emq.utils.BCrypt;
import com.ssd.emq.utils.JWTUtil;
import com.ssd.entity.Partner;
import com.ssd.entity.dto.LoginReq;
import com.ssd.entity.dto.PartnerReq;
import com.ssd.entity.dto.PartnerUpdatePwdReq;
import com.ssd.entity.vo.LoginResp;
import com.ssd.mapper.PartnerMapper;
import com.ssd.service.IPartnerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssd.view.BaseResponse;
import com.ssd.view.TokenObject;
import com.ssd.viewmodel.Pager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 * 合作商表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-14
 */
@Service
public class PartnerServiceImpl extends ServiceImpl<PartnerMapper, Partner> implements IPartnerService {
    @Resource
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public LoginResp login(LoginReq req) throws IOException {
        LoginResp resp = new LoginResp();
        resp.setSuccess(false);
        //1.校验验证码
        String code = redisTemplate.opsForValue().get(req.getClientToken()).toString();
        if (code == null) {
            resp.setMsg("验证码已过期！");
            return resp;
        }
        if (!code.equals(req.getCode())) {
            resp.setMsg("验证码不正确！");
            return resp;
        }
        //2.验证账号密码是否正确
        Partner partner = lambdaQuery().eq(Partner::getAccount, req.getAccount()).one();
        if (partner == null) {
            resp.setMsg("账户名或密码错误！");
            return resp;
        }
        boolean loginSuccess = BCrypt.checkpw(req.getPassword(), partner.getPassword());
        if (!loginSuccess) {
            resp.setMsg("账号名或密码错误！");
            return resp;
        }
        //3.生成token
        resp.setSuccess(true);
        resp.setUserId(partner.getId());
        resp.setUserName(partner.getName());
        resp.setMsg("登录成功！");

        TokenObject tokenObject = new TokenObject();
        tokenObject.setUserId(partner.getId());
        tokenObject.setUserName(partner.getName());
        tokenObject.setLoginType(VMSystem.LOGIN_PARTNER);
        tokenObject.setMobile(partner.getMobile());
        String token = JWTUtil.createJWTByObj(tokenObject,partner.getMobile() + VMSystem.JWT_SECRET);
        resp.setToken(token);
        return resp;
    }

    //重置密码
    @Override
    public BaseResponse resetPwd(Integer id) {
        return null;
    }

    //更新密码
    @Override
    public BaseResponse updatePwd(Integer id, PartnerUpdatePwdReq req) {
        return null;
    }

    //根据合作商id更新合作商数据
    @Override
    public BaseResponse modify(Long id, PartnerReq partner) {
        Partner bean = BeanUtil.toBean(partner, Partner.class);
        bean.setId(id);
        return new BaseResponse(updateById(bean));
    }

    @Override
    public BaseResponse<Boolean> delete(Integer id) {
        //1.远程调用售货机服务，看该合作商是否有点位 TODO

        //2.删除合作商数据
        return new BaseResponse<>(removeById(id));
    }

    @Override
    public BaseResponse<Pager<Partner>> search(Long pageIndex, Long pageSize, String name) {
        //1.设置分页条件
        Page<Partner> page = new Page<>(pageIndex,pageSize);
        LambdaQueryWrapper<Partner> qw = new LambdaQueryWrapper<>();
        if(!Strings.isNullOrEmpty(name)){
            qw.like(Partner::getName,name);
        }
        //2.查询
        this.page(page,qw);

        page.getRecords().forEach(p->{
            // 清除密码
            p.setPassword("");
            //远程调用获取售货机数量 TODO
//            p.setVmCount(vmService.getVmCountByOwnerId(p.getId()));
        });

        return new BaseResponse<>(Pager.build(page));
    }
}
